﻿using lwzlz.Application.System;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    public class AuthorityController : AdminController
    {
        #region 首页
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 角色权限
        [HttpPost]
        public JsonResult SaveRolePermission(int roleId, string menuIds)
        {
            ResponseModel response = new ResponseModel();
            AuthorityService service = new AuthorityService();
            try
            {
                List<int> menuIdList = new List<int>();
                if (string.IsNullOrEmpty(menuIds)) // 菜单ID列表为空（删除权限）
                {
                    service.SaveRolePermission(roleId, null);
                    response.Message = "删除成功";
                }
                else
                {
                    string[] menuIdArray = menuIds.Split(',');
                    foreach (string mid in menuIdArray)
                    {
                        int menuId = 0;
                        if (int.TryParse(mid, out menuId))
                        {
                            menuIdList.Add(menuId);
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "菜单ID错误";
                            return Json(response);
                        }
                    }
                    service.SaveRolePermission(roleId, menuIdList);

                    response.Message = "保存成功";
                }

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion

        #region 用户权限
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult SaveUserPermission(int userId, string menuIds)
        {
            ResponseModel response = new ResponseModel();
            AuthorityService service = new AuthorityService();
            try
            {
                List<int> menuIdList = new List<int>();
                if (string.IsNullOrEmpty(menuIds)) // 菜单ID列表为空（删除权限）
                {
                    service.SaveUserPermission(userId, null);

                    response.Message = "删除成功";
                }
                else
                {
                    string[] menuIdArray = menuIds.Split(',');
                    foreach (string mid in menuIdArray)
                    {
                        int menuId = 0;
                        if (int.TryParse(mid, out menuId))
                        {
                            menuIdList.Add(menuId);
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "菜单ID错误";
                            return Json(response);
                        }
                    }
                    service.SaveUserPermission(userId, menuIdList);

                    response.Message = "保存成功";
                }

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion
    }
}